/*
 * OPIAM Suite
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package opiam.admin.faare.struts.forms;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
import org.apache.struts.upload.MultipartRequestHandler;

import javax.servlet.http.HttpServletRequest;


/**
 * Based on apache struts 1.1. b3 example.
 *
 * This class is a placeholder for form values.  In a multipart request, files are represented by
 * set and get methods that use the class org.apache.struts.upload.FormFile, an interface with
 * basic methods to retrieve file information.  The actual structure of the FormFile is dependant
 * on the underlying impelementation of multipart request handling.  The default implementation
 * that struts uses is org.apache.struts.upload.CommonsMultipartRequestHandler.
 *
 * @author Mike Schachter
 * @version $Revision: 1.5 $ $Date: 2004/09/27 08:04:37 $
 */
public class UploadForm extends ActionForm
{
    /** Error message key. */
    public static final String ERROR_PROPERTY_MAX_LENGTH_EXCEEDED = "org.apache.struts.webapp.upload.MaxLengthExceeded";

    /**
     * The value of the embedded query string parameter.
     */
    private String queryParam;

    /**
     * The file that the user has uploaded.
     */
    private FormFile theFile;

    /**
     * Retrieve the value of the query string parameter.
     * @return query string parameter.
     */
    public String getQueryParam()
    {
        return queryParam;
    }

    /**
     * Set the value of the query string parameter.
     *
     * @param aqueryParam query string parameter.
     */
    public void setQueryParam(String aqueryParam)
    {
        this.queryParam = aqueryParam;
    }

    /**
     * Returns a representation of the file the user has uploaded.
     * @return  representation of the file the user has uploaded
     */
    public FormFile getTheFile()
    {
        return theFile;
    }

    /**
     * Sets a representation of the file the user has uploaded.
     * @param aFile a representation of the file the user has uploaded
     */
    public void setTheFile(FormFile aFile)
    {
        this.theFile = aFile;
    }

    /**
     * Check to make sure the client hasn't exceeded the maximum allowed upload size inside of this
     * validate method.
     *
     * @param mapping Struts mapping data
     * @param request HTTP request
     *
     * @return errors if any
     */
    @Override
	public ActionErrors validate(ActionMapping mapping,
        HttpServletRequest request)
    {
        ActionErrors errors = null;

        //has the maximum length been exceeded?
        Boolean maxLengthExceeded = (Boolean) request.getAttribute(
                           MultipartRequestHandler.ATTRIBUTE_MAX_LENGTH_EXCEEDED);

        if ((maxLengthExceeded != null) && (maxLengthExceeded.booleanValue()))
        {
            errors = new ActionErrors();
            errors.add(ERROR_PROPERTY_MAX_LENGTH_EXCEEDED,
                new ActionMessage("maxLengthExceeded"));
        }

        return errors;
    }
}
